//
//  ScrumDetailView.swift
//  codelabs
//
//  Created by 雷雷 on 25/05/2025.
//

import SwiftUI
import SwiftData

struct ScrumDetailView: View {
    
    let scrum:DailyScrum
    
    @State private var isEditing=false
    
    @State private var errorWrapper:ErrorWrapper?
    
    var body: some View {
        
        List{
            Section(header:Text("Meeting Info")){
                
                NavigationLink(
                    destination:MeetingView(
                        scrum:scrum,
                        errorWrapper:$errorWrapper,
                    )
                ){
                    Label("Start Meeting",systemImage: "waveform.and.person.filled")
                        .font(.headline)
                        .foregroundColor(.blue)
                        .bold()
                }
                
                HStack{
                    Label("Length",systemImage: "timer")
                    
                    Spacer()
                    
                    Text("\(scrum.lengthInMinutes) min") .foregroundStyle(.secondary)
                }
                .accessibilityElement(children: .combine)
                
                
                HStack {
                    Label("Attendees",systemImage: "rectangle.3.group.bubble")
                    Spacer()
                    Text("\(scrum.attendees.count)")
                        .foregroundStyle(.secondary)
                }
                .accessibilityElement(children: .combine)
                
                HStack{
                    
                    Label("Theme",systemImage: "moon.stars.fill")
                    
                    Spacer(minLength: 56.0)
                    
                    ThemeView(theme:scrum.theme)
                }
            }
            
            
            Section(header:Text("Attendees")){
                ForEach(scrum.attendees){attendee in
                    Label(attendee.name,systemImage: "person")
                }
                .font(.subheadline)
            }
            
            Section(header:Text("History")){
                
                if scrum.history.isEmpty==true {
                    Text("No history")
                }else{
                    ForEach(scrum.history){ scrumHistory in
                        HStack{
                            Image(systemName:"calendar")
                            Text(scrumHistory.date, style:.date)
                        }
                    }
                    .onDelete{ indicies in
                        scrum.history.remove(atOffsets: indicies)
                        do{
                            try scrum.modelContext?.save()
                        }catch{
                            
                        }
                    }
                }
            }
        }
        .navigationTitle(scrum.title)
        .toolbar{
            ToolbarItem(placement:.topBarTrailing){
                Button(action:{
                    isEditing=true
                }){
                    Image(systemName:"pencil.line")
                }
                .accessibilityLabel("Edit scrum")
                
            }
        }
        .sheet(isPresented:$isEditing){
            NavigationStack{
                ScrumEditor(scrum)
                    .navigationTitle(scrum.title)
            }
        }
        .sheet(item:$errorWrapper,onDismiss: nil){wrapper in
            ErrorView(errorWrapper:wrapper)
        }
    }
}

#Preview(traits: .dailyScrumSampleData){
    @Previewable @Query(sort:\DailyScrum.title) var scrums:[DailyScrum]
    
    NavigationStack{
        ScrumDetailView(scrum:scrums[0])
    }
    
}
